<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { getTestDetailById, getTestResult } from '@/services/healthTest.ts';
import type { TestDetail, TestResult } from '@/types/healthTest'

import { useMemberStore } from '@/stores'

const memberStore = useMemberStore()
const profile = memberStore.profile
console.log(profile?.patientId)
// 获取页面参数
const query = defineProps<{
  htId: string
  testTitle: string
}>()
// 定义数据
const messageList = ref<TestDetail[]>([])
const testResult = ref<TestResult>()

const select = async () => {
  uni.setNavigationBarTitle({
    title: query.testTitle,
  })
  const res = getTestDetailById(query.htId)
  messageList.value = (await res).data
}

const isChecked = (): boolean => {
  return messageList.value.every((item) => item.htResult !== null)
}
const testSubmit = async () => {
  if (isChecked()) {
    // 设置每个 TestDetail 的 patientId
    messageList.value.forEach((item) => {
      item.patientId = profile?.patientId // 添加 patientId
    })
    const res = await getTestResult(messageList.value)
    console.log(res)
    uni.navigateTo({
      url: `/pages/health/componets/ResultShow?testResultTitle=${res.testResultTitle}&testResult=${res.testResult}`,
    })
  } else {
    uni.showToast({
      title: '选项存在空缺',
      icon: 'error',
    })
  }
}
onMounted(select)
</script>
<template>
  <view class="box">
    <view class="yycs" v-for="(value, index) in messageList">
      <view class="text">{{ index + 1 + ',' + value.htQuestion }}</view>
      <uni-data-checkbox
        class="que"
        v-model="value.htResult"
        mode="list"
        icon="right"
        :localdata="[
          { text: value.questionY, value: 1 },
          { text: value.questionN, value: 0 },
        ]"
      ></uni-data-checkbox>
    </view>
    <view class="subtest" @tap="testSubmit">查看测试结果</view>
  </view>
</template>
<style lang="scss" scoped>
.box {
  background-color: #b3add3;
  padding: 15px;

  .yycs {
    background-color: #ffffff;
    padding: 10px;
    margin-bottom: 12px;

    .text {
      color: #7b4ab8;
      font-weight: bold;
    }

    .que {
      padding-top: 15px;
    }
  }

  .subtest {
    display: flex;
    align-items: center;
    /* 垂直居中 */
    justify-content: center;
    /* 水平居中 */
    width: 130px;
    height: 40px;
    border-radius: 15px;
    background-color: #572aea;
    color: #ebe5fc;
    margin-left: 30%;
    margin-top: 10px;
  }
}
</style>
